﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_UserManager_JobManager : PowerCreator.LMS.Web.Base.WebBasePage
{
    Job opJob = new Job();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            string search = GetSearch();
            BinderPG(search, 1);
            if (CheckUser())
            {
                btnAddNew.Enabled = true;
            }
            else
            {
                btnAddNew.Enabled = false;
            }
        }
    }

    private bool CheckUser()
    {
        bool rt = false;
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup == 1)
                {
                    rt = true;
                }
            }
        }

        return rt;

    }

    protected override void InitPage()
    {
        lbl_InfoTitle_JobManager.Text = GetLanageValueByKey("lbl_JobManager_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_JobManager.Text = GetLanageValueByKey("lbl_JobManager", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();

        btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
            btnAddNew.Visible = true;

    }
    private string GetSearch()
    {
        string search = string.Format("TeajobName like '%{0}%'", txtJobName.Text);
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = opJob.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search,int pageIndex)
    {
        DataTable dt = opJob.Query(search, LMSPageSize, pageIndex);
        rptJob.DataSource = dt;
        rptJob.DataBind();     
    }  

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddJob.aspx";
        Script.RedirectURL(url);
    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptRole_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "EditorJob.aspx?jobid=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //if (IsLicensed("DelJob"))
            //{
                if (opJob.Del(int.Parse(e.CommandArgument.ToString())))
                {
                    string search = GetSearch();
                    BinderPager(search);
                    BinderPG(search, AspNetPager1.CurrentPageIndex);
                }
            //}
            //else
            //{
            //    Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
            //    lbl_msg.Text = "您没有删除职位的权限!";
            //}
        }

    }
    protected void rptJob_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkeditor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            LinkButton lnkdel = (LinkButton)e.Item.FindControl("lnkbtn_del");
            if (CheckUser())
            {
                lnkeditor.Enabled = true;
                lnkdel.Enabled = true;
                lnkdel.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
            }
            else
            {
                lnkeditor.Enabled = false;
                lnkdel.Enabled = false;
            }
        }
    }
}
